//
//  WbStatusesPictureView.swift
//  Weibo
//
//  Created by HH on 16/12/7.
//  Copyright © 2016年 Weibo. All rights reserved.
//

import UIKit
import SnapKit

private let WbStatusesPictureCellID = "WbStatusesPictureCellID"

class WbStatusesPictureView: UICollectionView {
    
    private var flowLayout:UICollectionViewFlowLayout?
    
    var pictureViewModel:WbPictureViewModel = WbPictureViewModel.sharedPictureViewModel
    
    var pic_url:[WbStatusesPictureModel]?{
        
        didSet{
            
            descLabel.text = "\(pic_url?.count)"
            
            //在外部必须要给自身size,否则更新约束失败,也可以记录约束,删除约束,然后再添加约束
            if pic_url?.count == 1 {
                
                flowLayout?.itemSize = pictureViewModel.pictureViewSize!
                
//                self.snp.updateConstraints { (make) in
//                    make.size.equalTo(pictureViewModel.pictureViewSize ?? CGSize.zero)
//                }
            }else{
                
                flowLayout?.itemSize = CGSize(width: itemSizeWidth, height: itemSizeWidth)
                
            }
            
             self.snp.updateConstraints { (make) in
                make.size.equalTo(pictureViewModel.pictureViewSize ?? CGSize.zero)
            }
            
            layoutIfNeeded()
            
            //刷新数据
            reloadData()
        }
    }
    
    

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        flowLayout = UICollectionViewFlowLayout()
        
        flowLayout?.itemSize = CGSize(width: itemSizeWidth, height: itemSizeWidth)
        flowLayout?.minimumInteritemSpacing = itemMarign
        
        flowLayout?.minimumLineSpacing = itemMarign
        
        super.init(frame: frame, collectionViewLayout: flowLayout!)
        
        bounces = false
        

        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy private var descLabel:UILabel = {
       
        let label = UILabel()
        
        label.textColor = #colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1)
        
        label.backgroundColor = UIColor.red
        
        label.text = "9"
        
        return label
        
    }()
    
   
    private func setupUI(){
        
        addSubview(descLabel)
        
        descLabel.center = center
        
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        dataSource = self
        delegate = self
        
        register(WbStatusesPictureCell.self, forCellWithReuseIdentifier: WbStatusesPictureCellID)
    }
    
    

}

//遵守协议必须要实现代理方法,要不会报一个大错误
//数据源方法
extension WbStatusesPictureView:UICollectionViewDataSource{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return pic_url?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: WbStatusesPictureCellID, for: indexPath) as! WbStatusesPictureCell
        
        cell.pictureModel = pic_url?[indexPath.item]
        
        return cell
        
    }
    
    
}

extension WbStatusesPictureView:UICollectionViewDelegate{
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print(indexPath)
    }
}





